package com.spring.format.code.generation.util.file;

import java.util.Date;

import com.spring.format.code.generation.constants.Constants;
import com.spring.format.code.generation.constants.FileNameEnumType;
import com.spring.format.code.generation.mybatis.po.TablePO;
import com.spring.format.code.generation.mybatis.vo.CreateFileVO;
import com.spring.format.code.generation.util.FieldUtil;
import com.spring.format.code.generation.util.NameUtil;
import com.spring.format.code.generation.util.PathUtil;

/**
 * 文件生成帮助类
 * 
 * @author lenovo
 *
 */
public class CommonWebServiceFileUtil {
	private CommonWebServiceFileUtil() {

	}

	public static String content(CreateFileVO fileVO) {
		TablePO tablePO = fileVO.getTablePO();
		// vo的类名
		String voName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.VO_NAME);
		// vo的类名
		String formName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.FORM_NAME);
		// biz api名称
		String bizApiName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.BIZ_API_NAME);

		// biz impl 名称
		String bizImplName = NameUtil.createPrivateName(tablePO.getTable_name(), FileNameEnumType.BIZ_NAME);

		StringBuilder attribute = new StringBuilder();
		// 头部分
		attribute.append(head(tablePO, bizImplName, bizApiName));

		StringBuilder methodStr = new StringBuilder();

		// 新增数据
		methodStr.append(insert(voName, formName, bizImplName));
		// 编辑数据
		methodStr.append(update(formName, bizImplName));
		// 删除数据
		methodStr.append(delete(bizImplName));
		// 禁用
		methodStr.append(disable(bizImplName));
		// 启用
		methodStr.append(enable(bizImplName));
		// id查询
		methodStr.append(queryById(voName, formName, bizImplName));
		// 查询数据集合
		methodStr.append(queryList(voName, formName, bizImplName));
		// 查询分页
		methodStr.append(queryPage(voName, formName, bizImplName));

		attribute.append(methodStr).append("} ");

		return attribute.toString();

	}

	// 新增数据
	public static String head(TablePO tablePO, String bizImplName, String bizApiName) {
		StringBuilder attribute = new StringBuilder();
		attribute.append("package ")
				.append(PathUtil.createPackagePath(tablePO.getTable_name(), FileNameEnumType.WEB_SERVICE_NAME))
				.append("import java.util.List; \n").append("import java.util.Map; \n")
				.append("import org.springframework.stereotype.Service; \n ").append("import org.slf4j.Logger; \n ")
				.append("import org.slf4j.LoggerFactory; \n").append("import ")
				.append(PathUtil.createImportPath(tablePO.getTable_name(), FileNameEnumType.BIZ_API_NAME)).append(";\n")

				.append("/**\n").append("  *").append(tablePO.getTable_comment()).append("\n").append("  *@author ")
				.append(System.getProperties().getProperty("user.name")).append("\n").append("  *@version 1.0.0 ")
				.append(DateUtil.dateFormat(new Date(), DateUtil.DATE_FORMAT)).append(" \n").append("  */\n")
				.append("@Service(\"")
				.append(NameUtil.createSpringServiceName(
						NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.WEB_SERVICE_NAME)))
				.append("\") \n").append("public class ")
				.append(NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.WEB_SERVICE_NAME))
				.append("  implements ")
				.append(NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.WEB_SERVICE_API_NAME))
				.append("{ \n").append("		/*** 日志*/ \n")
				.append("		private Logger log = LoggerFactory.getLogger(this.getClass());\n \n")
				.append("		@Resource \n").append("		private ").append(bizApiName).append(" ")
				.append(bizImplName).append(";\n\n").append("		public ").append(bizApiName).append(" ")
				.append(FieldUtil.getMethod(bizImplName)).append("(){ \n").append("			return this.")
				.append(bizImplName).append(";\n").append("		}\n\n").append("		public void ")
				.append(FieldUtil.setMethod(bizImplName)).append("(").append(bizApiName).append(" ").append(bizImplName)
				.append("){ \n").append("			 this.").append(bizImplName).append(" = ").append(bizImplName)
				.append(";\n").append("		}\n \n \n");
		return attribute.toString();
	}

	// 新增数据
	public static String insert(String voName, String formName, String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" /** 1  \n ").append("  * 增加数据  \n ").append("  * @param vo \n ").append("  * @param loginVO \n ")
				.append("  */ \n ").append("@Override \n public ").append(Constants.RESINFO_VO).append("<?>")
				.append(Constants.SERVICE_METHOD_ADD).append("(").append(formName).append(" paraForm,")
				.append(Constants.CONSUMER_LOGIN_VO).append(" loginVO){\n ").append(Constants.RESINFO_VO)
				.append("<?> resultVO = new ").append(Constants.RESINFO_VO).append("<>(); \n")
				.append(" if(null==paraForm) { \n ").append(" log.info(\"参数为空paraForm={}\", paraForm); \n")
				.append(" resultVO.setMsg(\"参数错误\"); \n ").append(" return resultVO; \n").append(" } \n")
				.append(" //if(!paraForm.addValidate(resultVO)) { \n ").append("// return resultVO; \n ")
				.append("// } \n try{ \n").append("return \n").append(bizImplName).append(".")
				.append(Constants.SERVICE_METHOD_ADD).append("(paraForm,loginVO);\n")
                .append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}


	// 编辑数据
	public static String update(String formName, String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" /** 3  \n ").append("  * 编辑数据  \n ").append("  * @param vo \n ").append("  * @param loginVO \n ")
				.append("  */ \n ").append("@Override \n public ").append(Constants.RESINFO_VO).append("<?> ")
				.append(Constants.SERVICE_METHOD_UPDATE).append("(").append(formName).append(" paraForm, ")
				.append(Constants.CONSUMER_LOGIN_VO).append(" loginVO){\n try{ \n").append("return ").append(bizImplName)
				.append(".").append(Constants.SERVICE_METHOD_UPDATE).append("(paraForm,loginVO);\n")
				 .append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// 删除数据
	public static String delete(String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" /** 2 \n ").append("  * 根据id删除数据  \n ").append("  * @param id \n ")
				.append("  * @param loginVO \n ").append("  */ \n ").append("@Override \n public ")
				.append(Constants.RESINFO_VO).append("<?> ").append(Constants.SERVICE_METHOD_DELETE_BY_ID)
				.append("(Integer id,").append(Constants.CONSUMER_LOGIN_VO).append(" loginVO){\n try{ \n").append("return ")
				.append(bizImplName).append(".").append(Constants.SERVICE_METHOD_DELETE_BY_ID).append("(id,loginVO);\n")
				 .append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// 禁用
	public static String disable(String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" /** \n").append(" 禁用  \n ").append(" * @param id \n  * @param loginVO \n */ \n")
				.append("@Override \n public ").append(Constants.RESINFO_VO).append("<?> disable(Integer id,")
				.append(Constants.CONSUMER_LOGIN_VO).append(" loginVO){\n  try{ \n").append("return ").append(bizImplName)
				.append(".").append(Constants.SERVICE_METHOD_EDIT_STATUS).append("(1, id,loginVO);\n")
				 .append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// 启用
	public static String enable(String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" /** \n").append(" 启用  \n ").append(" * @param id \n  * @param loginVO \n */ \n")
				.append("@Override \n public ").append(Constants.RESINFO_VO).append("<?>  enable(Integer id,")
				.append(Constants.CONSUMER_LOGIN_VO).append(" loginVO){\n try{ \n").append("return ").append(bizImplName)
				.append(".").append(Constants.SERVICE_METHOD_EDIT_STATUS).append("(2, id,loginVO);\n")
				 .append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// id 查询
	public static String queryById(String voName, String formName, String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append("@Override \n public ").append(Constants.RESINFO_VO).append("<").append(voName).append("> ")
				.append(Constants.SERVICE_METHOD_QUERY_BY_ID).append("(Integer id){\n try{ \n").append("return ")
				.append(bizImplName).append(".").append(Constants.SERVICE_METHOD_QUERY_BY_ID).append("(id);\n")
				.append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// 查询集合
	public static String queryList(String voName, String formName, String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append("@Override \n public ").append(Constants.RESINFO_VO).append("<List<").append(voName).append(">> ")
				.append(Constants.SERVICE_METHOD_QUERY_LIST).append("(").append(formName).append(" paraForm){\n try{ \n")
				.append("return ").append(bizImplName).append(".").append(Constants.SERVICE_METHOD_QUERY_LIST)
				.append("(paraForm);\n")
				.append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}

	// 分页
	public static String queryPage(String voName, String formName, String bizImplName) {
		StringBuilder str = new StringBuilder();
		str.append("@Override \n  public ").append(Constants.RESINFO_VO).append("<List<").append(voName).append(">> ")
				.append(Constants.SERVICE_METHOD_QUERY_PAGE).append("(").append(formName).append(" paraForm){\n try{ \n")
				.append("return ").append(bizImplName).append(".").append(Constants.SERVICE_METHOD_QUERY_PAGE)
				.append("(paraForm);\n")
				.append( tryCatchEnd())
				.append("}\n");
		return str.toString();
	}
	
	
	// 方法调用 try 公用代码
	public static String tryCatchEnd() {
    	StringBuilder str = new StringBuilder();
    	str.append(" } catch (Exception e) { \n ")
		.append(" log.error(\"操作常见问题异常error={}\", e); \n")
		.append(" 	return new ").append(Constants.RESINFO_VO).append("<>(\"e.operator.is.fail\"); \n")
		.append(" } \n ");
    	
    	return str.toString();
    }
}
